package com.cssy.yd.basic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.basic.dao.BasicBomDAO;
import com.cssy.yd.basic.dao.BasicBomRalDAO;
import com.cssy.yd.basic.entity.BasicBomDO;
import com.cssy.yd.basic.entity.BasicBomRalDO;
import com.cssy.yd.basic.entity.BasicMaterialstypeDO;
import com.cssy.yd.basic.service.IbasicBomService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("basicBomService")
public class BasicBomServiceImpl implements IbasicBomService {
	private static final String itemtype="BOM组";
	@Resource(name="basicBomDAO")
	private BasicBomDAO basicBomDAO;
	
	@Resource(name="basicBomRalDAO")
	private BasicBomRalDAO basicBomRalDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicBomDO>  list=basicBomDAO.find(scs);
		rs.setRecordCount(basicBomDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		basicBomDAO.deleteBasicBomDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(BasicBomDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return basicBomDAO.updateBasicBomDO(t);
	}

	@Override
	public BasicBomDO save(BasicBomDO t, User user) throws SyBaseException {
		Date date=new Date();
		t.setDcruser(user.getName());
		t.setDctime(date);
		t.setId(IDCodeTypeUtil.getId());
		basicBomDAO.insertBasicBomDO(t);
		return t;
	}

	@Override
	public BasicBomDO findListById(String id) throws SyBaseException {

		return basicBomDAO.findBasicBomDOByPrimaryKey(id);
	}

	@Override
	public List<BasicBomDO> findList(BasicBomDO basicBomDO)
			throws SyBaseException {
		List<BasicBomDO>  list =basicBomDAO.findListByExample(basicBomDO);
		return list;
	}

	@Override
	public List<BasicBomDO> findAll(String s) throws SyBaseException {
		
		return basicBomDAO.findAll(s);
	}

	@Override
	public List<BasicBomDO> findAllPa(String s) throws SyBaseException {
		
		return basicBomDAO.findAllPa(s);
	}

	@Override
	public void bomSave(HttpServletRequest request, BasicBomDO basicBomDO)
			throws SyBaseException {
		try {
			String itemdata =request.getParameter("itemdata");
			String itemdelete =request.getParameter("itemdelete");
			Integer type =Integer.parseInt(request.getParameter("type"));
			basicBomDO.setBillstatus(type);
			List<BasicBomRalDO> bbrdate = JSONArray.parseArray(itemdata,
					BasicBomRalDO.class);
			if(basicBomDO.getId() != null && !"".equals(basicBomDO.getId())){
				basicBomDAO.updateBasicBomDO(basicBomDO);
					if (itemdelete != null && !"".equals(itemdelete)) {
						List<BasicBomRalDO> bbrdel = JSONArray.parseArray(itemdelete, BasicBomRalDO.class);
						for (int i = 0; i < bbrdel.size(); i++) {
							if (bbrdel.get(i).getId() != null&& !"".equals(bbrdel.get(i).getId())) {
								basicBomRalDAO.deleteBasicBomRalDOByPrimaryKey(bbrdel.get(i).getId());
							}
						}
					}
					for (int i = 0; i < bbrdate.size(); i++) {
						bbrdate.get(i).setSunid(basicBomDO.getId());
						bbrdate.get(i).setSunname(basicBomDO.getTurnoffName());
						if (bbrdate.get(i).getId() != null&& !"".equals(bbrdate.get(i).getId())) {
							basicBomRalDAO.updateBasicBomRalDO(bbrdate.get(i));
						} else {
							bbrdate.get(i).setId(IDCodeTypeUtil.getId());
							basicBomRalDAO.insertBasicBomRalDO(bbrdate.get(i));
						}
					}
			}else{
				String id = IDCodeTypeUtil.getId();
				basicBomDO.setTurnoffCode(syCodeService.getCode("BOM","BOM",SyCode.DATE_YYMM,5));
				basicBomDO.setDcruser(SessionUtil.getUser(request).getName());
				basicBomDO.setDctime(new Date());
				basicBomDO.setId(id);
				SearchConditions scs = new SearchConditions();
				int count = basicBomDAO.count(scs);
				if(count>0){
					String parentnameadd=request.getParameter("parentnameadd");
					String parentadd=request.getParameter("parentadd");
					basicBomDO.setParentid(parentadd);
					basicBomDO.setParentTurnoffName(parentnameadd);
					basicBomDAO.insertBasicBomDO(basicBomDO);
				}else{
					basicBomDO.setTurnoffName(itemtype);
					basicBomDAO.insertBasicBomDO(basicBomDO);
				}
				
				if (bbrdate.size() > 0){
					for (int i = 0;i < bbrdate.size();i++){
						bbrdate.get(i).setId(IDCodeTypeUtil.getId());
						bbrdate.get(i).setSunid(id);
						bbrdate.get(i).setSunname(basicBomDO.getTurnoffName());
						basicBomRalDAO.insertBasicBomRalDO(bbrdate.get(i));
					}
				}
			}			
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
		
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}


}
